package com.mengya.interrupt;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @ClassName InterruptDemo
 * @Description 如何优雅退出线程？
 * 三种方式：volatile；AtomicBoolean；interrupt
 * @Author xiongwei.wu
 * @Date 2023/1/9 10:27
 **/
public class InterruptDemo {

    static volatile boolean isStop = false;
    static AtomicBoolean atomicBoolean = new AtomicBoolean(false);

    public static void main(String[] args) throws InterruptedException {

//        m1();
//        m2();
//        m3();
//        m4();
//        m5();
        System.out.println(Thread.currentThread().getName() + "--------" + Thread.interrupted());
        System.out.println(Thread.currentThread().getName() + "--------" + Thread.interrupted());
        System.out.println(111111);
        Thread.currentThread().interrupt();
        System.out.println(222222);
        System.out.println(Thread.currentThread().getName() + "---------" + Thread.interrupted());
        System.out.println(Thread.currentThread().getName() + "---------" + Thread.interrupted());

    }

    private static void m5() {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("-----------isStop = true,程序结束。");
                    break;
                }
                //异常情况下中断会失败！  InterruptedException
                try {
                    Thread.sleep(500);
                } catch (InterruptedException exception) {
                    Thread.currentThread().interrupt();
                    exception.printStackTrace();
                }
                System.out.println("-----------hello isStop");
            }
        }, "t1");
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(3);
        } catch (InterruptedException exception) {
            exception.printStackTrace();
        }

        //修改t1线程的中断标志位为TRUE
        new Thread(t1::interrupt, "t2").start();

        System.out.println("************" + t1.isInterrupted());
    }

    /**
     * 中断为TRUE后，不会立即停止线程
     *
     * @throws InterruptedException
     */
    private static void m4() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 300; i++) {
                System.out.println("-----------i: " + i);
            }
            System.out.println("t1.interrupt()调用之后02：" + Thread.currentThread().isInterrupted());
        }, "t1");
        t1.start();

        System.out.println("t1.interrupt()调用之前，t1线程的中断标识默认值：" + t1.isInterrupted());
        TimeUnit.MILLISECONDS.sleep(3);
        //实例方法interrupt()仅仅是设置线程的“”“”“”“”“”中断状态设置为TRUE，不会停止线程”“”“”“”“”
        t1.interrupt();
        //活动状态，t1线程还在执行中
        System.out.println("t1.interrupt()调用之后01：" + t1.isInterrupted());

        TimeUnit.MILLISECONDS.sleep(3000);
        //非活动状态，t1线程不在执行中，已经结束执行了
        System.out.println("t1.interrupt()调用之后03：" + t1.isInterrupted());
    }

    /**
     * 通过interrupt中断标志位
     */
    private static void m3() {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("-----------isStop = true,程序结束。");
                    break;
                }
                System.out.println("-----------hello isStop");
            }
        }, "t1");
        t1.start();

        System.out.println("**************" + t1.isInterrupted());

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException exception) {
            exception.printStackTrace();
        }

        //修改t1线程的中断标志位为TRUE
        new Thread(t1::interrupt, "t2").start();

        System.out.println("************" + t1.isInterrupted());
    }

    /**
     * 通过atomic
     */
    private static void m2() {
        new Thread(() -> {
            while (true) {
                if (atomicBoolean.get()) {
                    System.out.println("-----------isStop = true,程序结束。");
                    break;
                }
                System.out.println("-----------hello isStop");
            }
        }, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException exception) {
            exception.printStackTrace();
        }

        new Thread(() -> {
            atomicBoolean.set(true);
        }, "t2").start();
    }

    /**
     * 通過volatile 关键字
     */
    private static void m1() {
        new Thread(() -> {
            while (true) {
                if (isStop) {
                    System.out.println("-----------isStop = true,程序结束。");
                    break;
                }
                System.out.println("-----------hello isStop");
            }
        }, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException exception) {
            exception.printStackTrace();
        }

        new Thread(() -> {
            isStop = true;
        }, "t2").start();
    }
}
